package com.zack.gank.gankandroid.main.main;

import com.gank.zack.ganklibrary.network.retrofit.RetrofitHelper;
import com.orhanobut.logger.Logger;
import com.zack.gank.gankandroid.api.GankInterface;
import com.zack.gank.gankandroid.main.find.FindAndroidView;
import com.zack.gank.gankandroid.main.find.bean.AndroidGank;
import com.zack.gank.gankandroid.main.find.bean.ImageGank;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Android on 2017/7/14.
 */
public class SexyPresenter implements SexyView.Presenter {

    SexyView findView;
    public SexyPresenter(SexyView findView) {
        this.findView=findView;
    }


    @Override
    public void setGankNumPager(String type,String number, String pager) {
        GankInterface gankInterface= RetrofitHelper.createApi(GankInterface.class);
        getSexyData(gankInterface, type, number, pager);
    }


    /**
     * 获取默认图片 在没有图片的情况下
     * @param gankInterface
     */
    private void getSexyData(GankInterface gankInterface,String type,String number,String pager){
        /*Call<ImageGank> objectCall=gankInterface.getGankImgeDatas(type, number, pager);
        objectCall.enqueue(new Callback<ImageGank>() {
            @Override
            public void onResponse(Call<ImageGank> call, Response<ImageGank> response) {
                findView.onSucess(response.body());
            }
            @Override
            public void onFailure(Call<ImageGank> call, Throwable t) {
                findView.onFile(t);
            }
        });*/
        Observable<ImageGank> objectCall=gankInterface.getGankImgeData(type, number, pager);
        objectCall.subscribeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                     .doOnNext(new Action1<ImageGank>() {
                         @Override
                         public void call(ImageGank imageGank) {

                         }
                     })
                     .subscribeOn(AndroidSchedulers.mainThread())
                     .subscribe(new Subscriber<ImageGank>() {
                         @Override
                         public void onCompleted() {

                         }

                         @Override
                         public void onError(Throwable e) {
                             findView.onFile(e);
                         }

                         @Override
                         public void onNext(ImageGank imageGank) {
                             findView.onSucess(imageGank);
                         }
                     });
    }
}
